package com.originskyseed.platform.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.cms.mapper.CmsTypeMapper;
import com.originskyseed.platform.cms.model.CmsType;
import com.originskyseed.platform.cms.service.ICmsTypeService;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;

/**
 * 新闻类型表Service接口实现类（OSI_CMS_TYPE）
 *
 * @author wlj
 */
@DubboService
@Component("cmsTypeService")
public class CmsTypeServiceImpl implements ICmsTypeService {

	@Resource
	private CmsTypeMapper cmsTypeMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.cmsTypeMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.cmsTypeMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(CmsType record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.cmsTypeMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(CmsType record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		Integer prority = this.cmsTypeMapper.selectMaxPrority();
		if (prority == null) {
			record.setPrority(0);
		} else {
			record.setPrority(prority + 1);
		}
		record.setCreateTime(new Date());
		return this.cmsTypeMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(CmsType record) throws Exception {
		record.setUpdateTime(new Date());
		return this.cmsTypeMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(CmsType record) throws Exception {
		record.setUpdateTime(new Date());
		return this.cmsTypeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public CmsType selectByPrimaryKey(String id) throws Exception {
		return this.cmsTypeMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询名称是否存在
	 *
	 * @param name
	 * @return
	 * @throws Exception
	 */
	@Override
	public CmsType selectIsNullByTypeName(String name) throws Exception {
		return this.cmsTypeMapper.selectIsNullByTypeName(name);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CmsType> selectList(CmsType record) throws Exception {
		return this.cmsTypeMapper.selectList(record);
	}

	/**
	 * 查询新闻类型树
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CmsType> cmsTypeTree(Map<String, Object> params) throws Exception {
		/*
		 * List<CmsType> cmsTypeList = new ArrayList<>(); Integer Platform[] = {0,1,2}; for (Integer i : Platform) { CmsType cmsType = new CmsType(); cmsType.setBelongPlatform(i); cmsTypeList.add(cmsType); }
		 */
		/*
		 * if(cmsTypeList != null && cmsTypeList.size() > 0){ for (CmsType cmsType : cmsTypeList) { cmsType.setIsDel(2); cmsType.setType(1); List<CmsType> cmsTypeTreeList = this.cmsTypeMapper.selectList(cmsType); cmsType.setCmsTypeList(cmsTypeTreeList); } }
		 */
		List<CmsType> cmsTypeList = this.cmsTypeMapper.selectListByPlatform(params);
		return cmsTypeList;
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CmsType> selectListByPage(Map<String, Object> params, BasePage<CmsType> page) throws Exception {
		params.put("page", page);
		List<CmsType> list = this.cmsTypeMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 更新是否置顶
	 *
	 * @param cmsType
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateIsTop(CmsType cmsType) throws Exception {
		cmsType.setUpdateTime(new Date());
		this.cmsTypeMapper.updateIsTop(cmsType);
		return this.cmsTypeMapper.updateByPrimaryKeySelective(cmsType);
	}

	/**
	 * 根据优先级查询新闻
	 *
	 * @return
	 * @throws Exception
	 */
	public CmsType selectCmsTypeByPrority(Integer prority) throws Exception {
		return cmsTypeMapper.selectCmsTypeByPrority(prority);
	}

	/**
	 * 更新排序信息
	 *
	 * @param downCmsType
	 * @param upCmsType
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updatePrority(CmsType downCmsType, CmsType upCmsType) throws Exception {
		downCmsType.setUpdateTime(new Date());
		upCmsType.setUpdateTime(new Date());
		this.cmsTypeMapper.updateByPrimaryKeySelective(downCmsType);
		return this.cmsTypeMapper.updateByPrimaryKeySelective(upCmsType);
	}
}
